package com.example.xiaohei.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.*
import com.example.xiaohei.R
import com.example.xiaohei.databinding.FragmentCategoryBinding
import com.example.xiaohei.databinding.FragmentRecommendBinding
import com.example.xiaohei.util.LogUtil
import com.example.xiaohei.viewmodel.CategoryViewModel
import com.example.xiaohei.viewmodel.ReCommendViewModel

class CategoryFragment :Fragment(), LifecycleObserver{

    lateinit var binding: FragmentCategoryBinding

    val viewModel by lazy {
        ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory(requireActivity().application)).get(
            CategoryViewModel::class.java)
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate<FragmentCategoryBinding>(inflater, R.layout.fragment_category,container,false)
        binding.viewModel = viewModel
        initData()
        observer()
        return binding.root
    }

    private fun initData(){
        lifecycleScope.launchWhenCreated {
            viewModel.getProvinces()

        }
    }

    private fun observer(){
        viewModel.provinces.observe(viewLifecycleOwner, {
            LogUtil.e("嘿嘿嘿")
        })
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun myOnStar() {
        print("onStar")
    }



}